/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.Vector;

import Models.KhuyenMai_Model;
import Models.NhatKyHT_Model;
import Models.NhomMA_Model;
import Models.MonAn_Model;
import Beans.NhanVien;

/**
 *
 * @author Venus
 */
public class KhuyenMai_Controller extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            String action = request.getParameter("action");
            int langid = (Integer) session.getAttribute("langid");
            if (action.equals("Show")) {
                String item = request.getParameter("item");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.LayDSKM(langid); // Lấy danh sách khu vực
                request.setAttribute("DSKM", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listpromotion.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("ShowAdd")) {
                String item = request.getParameter("item");
                String malkm = request.getParameter("malkm");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector vDSLKM = new Vector();
                vDSLKM = kmm.LayDSLKMTao(langid, malkm);
                Vector vDSNT = new Vector();
                vDSNT = kmm.LayDSNT(langid);
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMA(langid);
                request.setAttribute("item", item);
                request.setAttribute("malkm", malkm);
                request.setAttribute("DSLKM", vDSLKM);
                request.setAttribute("DSNT", vDSNT);
                request.setAttribute("DSNMA", vDSNMA);
                RequestDispatcher rd = request.getRequestDispatcher("admin/newpromotion.jsp");
                rd.forward(request, response);
            }            
            else if (action.equals("ShowDSMA")) {
                String manma = request.getParameter("manma");
                String item = request.getParameter("item");
                KhuyenMai_Model kmm = new KhuyenMai_Model();                
                Vector vDSMA = new Vector();
                vDSMA = MonAn_Model.LayDSMATheoNMA(langid, Integer.parseInt(manma));
                Vector vDSNT = new Vector();
                vDSNT = kmm.LayDSNT(langid);                
                request.setAttribute("DSMA", vDSMA);
                request.setAttribute("DSNT", vDSNT);
                request.setAttribute("item", item);
                request.setAttribute("manma", manma);
                RequestDispatcher rd = request.getRequestDispatcher("admin/tablelistfood.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("addpromotion")) {
                String loaikm = request.getParameter("loaikm");
                String datestart = request.getParameter("datestart");
                String enddate = request.getParameter("enddate");
                String promotionname = request.getParameter("promotionname");
                String content = request.getParameter("content");
                int MaKM = KhuyenMai_Model.TaoMa();
                KhuyenMai_Model.ThemKhuyenMai(MaKM, promotionname, loaikm, datestart, enddate, content );
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.LayKMEdit(MaKM, langid);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "promotion");
                request.setAttribute("KM", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/promotiondate.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("AddPromotionBill")) {
                String loaikm = request.getParameter("loaikm");
                String datestart = request.getParameter("datestart");
                String enddate = request.getParameter("enddate");
                String promotionname = request.getParameter("promotionname");
                String content = request.getParameter("content");
                String condition = request.getParameter("condition");
                String discount = request.getParameter("discount");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                int MaKM = KhuyenMai_Model.TaoMa();                
                KhuyenMai_Model.ThemKhuyenMai(MaKM, promotionname, loaikm, datestart, enddate, content );
                KhuyenMai_Model.ThemKMTHD( MaKM, condition, discount );
                String item = request.getParameter("item");
                Vector v = new Vector();
                v = kmm.LayDSKM(langid);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "promotion");
                NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "promotionbill");
                request.setAttribute("DSKM", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listpromotion.jsp");
                rd.forward(request, response);
            }                      
            else if (action.equals("SortTenKM")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.SortTenKM(langid, order);
                request.setAttribute("DSKM", v);                
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/sortpromotion.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortNgayBDKM")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.SortNgayBDKM(langid, order);
                request.setAttribute("DSKM", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/sortpromotion.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortNgayKTKM")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.SortNgayKTKM(langid, order);
                request.setAttribute("DSKM", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/sortpromotion.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortTTKM")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.SortTTKM(langid, order);
                request.setAttribute("DSKM", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "4");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/sortpromotion.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SearchListKM")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String TENKM = request.getParameter("TENKM");
                String BATDAU = request.getParameter("BATDAU");
                String KETTHUC = request.getParameter("KETTHUC");
                String TINHTRANG = request.getParameter("TINHTRANG");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.SearchListKM(TENKM, BATDAU, KETTHUC, TINHTRANG, langid);
                request.setAttribute("DSKM", v);
                request.setAttribute("item", item);
                request.setAttribute("TENKM", TENKM);
                request.setAttribute("BATDAU", BATDAU);
                request.setAttribute("KETTHUC", KETTHUC);
                request.setAttribute("TINHTRANG", TINHTRANG);
                RequestDispatcher rd = request.getRequestDispatcher("admin/searchpromotion.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchTenKM")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                String TENKM = request.getParameter("TENKM");
                String BATDAU = request.getParameter("BATDAU");
                String KETTHUC = request.getParameter("KETTHUC");
                String TINHTRANG = request.getParameter("TINHTRANG");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.SortSearchTenKM(TENKM, BATDAU, KETTHUC, TINHTRANG, langid, order);
                request.setAttribute("DSKM", v);
                request.setAttribute("TENKM", TENKM);
                request.setAttribute("BATDAU", BATDAU);
                request.setAttribute("KETTHUC", KETTHUC);
                request.setAttribute("TINHTRANG", TINHTRANG);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/sortsearchpromotion.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchNgayBDKM")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                String TENKM = request.getParameter("TENKM");
                String BATDAU = request.getParameter("BATDAU");
                String KETTHUC = request.getParameter("KETTHUC");
                String TINHTRANG = request.getParameter("TINHTRANG");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.SortSearchNgayBDKM(TENKM, BATDAU, KETTHUC, TINHTRANG, langid, order);
                request.setAttribute("DSKM", v);
                request.setAttribute("TENKM", TENKM);
                request.setAttribute("BATDAU", BATDAU);
                request.setAttribute("KETTHUC", KETTHUC);
                request.setAttribute("TINHTRANG", TINHTRANG);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/sortsearchpromotion.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchNgayKTKM")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                String TENKM = request.getParameter("TENKM");
                String BATDAU = request.getParameter("BATDAU");
                String KETTHUC = request.getParameter("KETTHUC");
                String TINHTRANG = request.getParameter("TINHTRANG");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.SortSearchNgayKTKM(TENKM, BATDAU, KETTHUC, TINHTRANG, langid, order);
                request.setAttribute("DSKM", v);
                request.setAttribute("TENKM", TENKM);
                request.setAttribute("BATDAU", BATDAU);
                request.setAttribute("KETTHUC", KETTHUC);
                request.setAttribute("TINHTRANG", TINHTRANG);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/sortsearchpromotion.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchTTKM")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                String TENKM = request.getParameter("TENKM");
                String BATDAU = request.getParameter("BATDAU");
                String KETTHUC = request.getParameter("KETTHUC");
                String TINHTRANG = request.getParameter("TINHTRANG");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.SortSearchTTKM(TENKM, BATDAU, KETTHUC, TINHTRANG, langid, order);
                request.setAttribute("DSKM", v);
                request.setAttribute("TENKM", TENKM);
                request.setAttribute("BATDAU", BATDAU);
                request.setAttribute("KETTHUC", KETTHUC);
                request.setAttribute("TINHTRANG", TINHTRANG);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/sortsearchpromotion.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("ShowLangKM")) {
                String makm = request.getParameter("makm");
                KhuyenMai_Model kvm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kvm.LayKM(Integer.parseInt(makm));
                request.setAttribute("LangKM", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/promotionlang.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("EditLang")) {
                String makm = request.getParameter("makm");
                String TenKM = request.getParameter("TenKM");
                String NoiDung = request.getParameter("NoiDung");
                String mann = request.getParameter("mann");
                KhuyenMai_Model.Edit(makm, mann, TenKM, NoiDung);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "promotion");
                KhuyenMai_Model kvm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kvm.LayKM(Integer.parseInt(makm));
                request.setAttribute("LangKM", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/promotionlang.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("UpdateStatusKM")) {
                String makm = request.getParameter("makm");
                String status = request.getParameter("status");
                String item = request.getParameter("item");
                KhuyenMai_Model.UpdateStatusKM(makm, status);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "statuspromotion");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.LayDSKM(langid);
                request.setAttribute("DSKM", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listpromotion.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("KMTheoHoaDon")){
                String malkm = request.getParameter("malkm");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector vDSLKM = new Vector();
                vDSLKM = kmm.LayDSLKMTao(langid, malkm);
                request.setAttribute("malkm", malkm);
                request.setAttribute("DSLKM", vDSLKM);
                RequestDispatcher rd = request.getRequestDispatcher("admin/promotionbill.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("UpdatePromotion")){
                String makm = request.getParameter("makm");
                String item = request.getParameter("item");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.LayKMEdit(Integer.parseInt(makm), langid);
                int MaLKM = KhuyenMai_Model.CheckLoaiKM(makm, langid);
                if(MaLKM == 1){
                    request.setAttribute("item", item);
                    request.setAttribute("makm", makm);
                    request.setAttribute("KM", v);
                    RequestDispatcher rd = request.getRequestDispatcher("admin/prodate.jsp");
                    rd.forward(request, response);
                }
                else{
                    request.setAttribute("item", item);
                    request.setAttribute("makm", makm);
                    request.setAttribute("KM", v);
                    RequestDispatcher rd = request.getRequestDispatcher("admin/probill.jsp");
                    rd.forward(request, response);
                }
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "promotion");
            }
            else if(action.equals("DetailPromotionDay")){
                String makm = request.getParameter("makm");
                KhuyenMai_Model kmm = new KhuyenMai_Model();                
                Vector vDSNT = new Vector();
                vDSNT = kmm.LayDSNT(langid);
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMAA(langid);
                request.setAttribute("DSNT", vDSNT);
                request.setAttribute("DSNMA", vDSNMA);
                request.setAttribute("makm", makm);
                RequestDispatcher rd = request.getRequestDispatcher("admin/detailproday.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("ShowDetailPromotionDay")){
                String makm = request.getParameter("makm");
                String item = request.getParameter("item");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector vDSNT = new Vector();
                vDSNT = kmm.LayDSNT(langid);
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMAA(langid);
                Vector vDSKMTN = new Vector();
                vDSKMTN = kmm.LayDSKMTN(Integer.parseInt(makm));
                request.setAttribute("DSKMTN", vDSKMTN);
                request.setAttribute("DSNT", vDSNT);
                request.setAttribute("DSNMA", vDSNMA);
                request.setAttribute("makm", makm);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/showdetailproday.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("AddProDay")){
                String makm = request.getParameter("makm");
                String discount = request.getParameter("discount");
                String mamonan = request.getParameter("mamonan");
                String mangay = request.getParameter("mangay");
                String item = request.getParameter("item");
                String mama[] = mamonan.split(";");
                String mangaythu[] = mangay.split(";");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                int i = 0;
                while (i < mama.length) {
                    if(mangaythu.length == 7){
                        KhuyenMai_Model.ThemKMTN(makm, "0", mama[i], discount);                        
                    }
                    else{
                        for(int j=0; j<mangaythu.length; j++){
                            KhuyenMai_Model.ThemKMTN(makm, mangaythu[j], mama[i], discount);
                        }
                    }
                    i = i + 1;
                }
                Vector vDSKMTN = new Vector();
                vDSKMTN = kmm.LayDSKMTN(Integer.parseInt(makm));
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "promotionday");
                request.setAttribute("DSKMTN", vDSKMTN);
                request.setAttribute("makm", makm);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/detailpromotionday.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("CheckAddProDay")){
                String makm = request.getParameter("makm");                
                String mamonan = request.getParameter("mamonan");
                String mangay = request.getParameter("mangay");
                String mama[] = mamonan.split(";");
                String mangaythu[] = mangay.split(";");                
                int i = 0;                
                while (i < mama.length) {
                    if(mangaythu.length == 7){
                        if( KhuyenMai_Model.CheckNgayThu(Integer.parseInt(makm), 0 , Integer.parseInt(mama[i]))){
                            out.print("exist");
                            break;
                        }
                        if( KhuyenMai_Model.CheckNgayThuCaTuan(Integer.parseInt(makm), Integer.parseInt(mama[i]))){
                            out.print("exist");
                            break;
                        }
                    }
                    else{
                        for(int j=0; j<mangaythu.length; j++){
                            if( KhuyenMai_Model.CheckNgayThu(Integer.parseInt(makm), 0 , Integer.parseInt(mama[i]))){
                                out.print("exist");
                                break;
                            }
                            if( KhuyenMai_Model.CheckNgayThu(Integer.parseInt(makm), Integer.parseInt(mangaythu[j]) , Integer.parseInt(mama[i]))){
                                out.print("exist");
                                break;
                            }
                        }
                    }                   
                   i = i + 1;
                }
            }
            else if(action.equals("DeleteProDay")){
                String makmtn = request.getParameter("makmtn");
                String makm = request.getParameter("makm");
                String item = request.getParameter("item");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                KhuyenMai_Model.DeleteProDay(Integer.parseInt(makmtn));
                Vector vDSKMTN = new Vector();
                vDSKMTN = kmm.LayDSKMTN(Integer.parseInt(makm));
                request.setAttribute("DSKMTN", vDSKMTN);
                request.setAttribute("makm", makm);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/detailpromotionday.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("ShowListFood")){
                String manma = request.getParameter("manma");
                String item = request.getParameter("item");
                Vector vDSMA = new Vector();
                vDSMA = MonAn_Model.LayDSMATheoNMA(langid,Integer.parseInt(manma) );
                request.setAttribute("DSMA", vDSMA);
                request.setAttribute("item", item);
                request.setAttribute("manma", manma);                
                RequestDispatcher rd = request.getRequestDispatcher("admin/showlistmonan.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("ViewDetailProDay")){
                String makm = request.getParameter("makm");
                String item = request.getParameter("item");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector vDSKMTN = new Vector();
                vDSKMTN = kmm.LayDSKMTN(Integer.parseInt(makm));
                request.setAttribute("DSKMTN", vDSKMTN);
                request.setAttribute("makm", makm);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/detailpromotionday.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortTenMAKM")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                String makm = request.getParameter("makm");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.SortTenMAKM(langid, order, makm);
                request.setAttribute("DSKMTN", v);
                request.setAttribute("item", item);
                request.setAttribute("makm", makm);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/sortaddpromotion.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortNgayKM")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                String makm = request.getParameter("makm");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.SortNgayKM(langid, order, makm);
                request.setAttribute("DSKMTN", v);
                request.setAttribute("item", item);
                request.setAttribute("makm", makm);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/sortaddpromotion.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortGiaKM")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                String makm = request.getParameter("makm");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.SortGiaKM(langid, order, makm);
                request.setAttribute("DSKMTN", v);
                request.setAttribute("item", item);
                request.setAttribute("makm", makm);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/sortaddpromotion.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SearchAddPro")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String tenma = request.getParameter("tenma");
                String ngaythu = request.getParameter("ngaythu");
                String gia = request.getParameter("gia");
                String makm = request.getParameter("makm");
                String giamgia = request.getParameter("giamgia");

                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.SearchAddPro(tenma, ngaythu, gia, giamgia, makm, langid);
                request.setAttribute("DSKMTN", v);
                request.setAttribute("item", item);
                request.setAttribute("tenma", tenma);
                request.setAttribute("ngaythu", ngaythu);
                request.setAttribute("gia", gia);
                request.setAttribute("giamgia", giamgia);
                request.setAttribute("makm", makm);
                RequestDispatcher rd = request.getRequestDispatcher("admin/searchaddpro.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchTenMAKM")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                String tenma = request.getParameter("tenma");
                String ngaythu = request.getParameter("ngaythu");
                String gia = request.getParameter("gia");
                String makm = request.getParameter("makm");
                String giamgia = request.getParameter("giamgia");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.SortSearchTenMAKM(tenma, ngaythu, gia, giamgia, makm, langid, order);
                request.setAttribute("DSKMTN", v);
                request.setAttribute("item", item);
                request.setAttribute("makm", makm);
                request.setAttribute("tenma", tenma);
                request.setAttribute("ngaythu", ngaythu);
                request.setAttribute("gia", gia);
                request.setAttribute("giamgia", giamgia);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/sortsearchaddpro.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchNgayKM")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                String tenma = request.getParameter("tenma");
                String ngaythu = request.getParameter("ngaythu");
                String gia = request.getParameter("gia");
                String makm = request.getParameter("makm");
                String giamgia = request.getParameter("giamgia");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.SortSearchNgayKM(tenma, ngaythu, gia, giamgia, makm, langid, order);
                request.setAttribute("DSKMTN", v);
                request.setAttribute("item", item);
                request.setAttribute("makm", makm);
                request.setAttribute("tenma", tenma);
                request.setAttribute("ngaythu", ngaythu);
                request.setAttribute("gia", gia);
                request.setAttribute("giamgia", giamgia);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/sortsearchaddpro.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchGiaKM")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                String tenma = request.getParameter("tenma");
                String ngaythu = request.getParameter("ngaythu");
                String gia = request.getParameter("gia");
                String makm = request.getParameter("makm");
                String giamgia = request.getParameter("giamgia");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.SortSearchGiaKM(tenma, ngaythu, gia, giamgia, makm, langid, order);
                request.setAttribute("DSKMTN", v);
                request.setAttribute("item", item);
                request.setAttribute("makm", makm);
                request.setAttribute("tenma", tenma);
                request.setAttribute("ngaythu", ngaythu);
                request.setAttribute("gia", gia);
                request.setAttribute("giamgia", giamgia);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/sortsearchaddpro.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("UpdateGiamGia")){
                String makm = request.getParameter("makm");
                String makmtn = request.getParameter("makmtn");
                String giamgia = request.getParameter("giamgia");
                String item = request.getParameter("item");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                KhuyenMai_Model.UpdateGiamGia( Integer.parseInt(makmtn), Integer.parseInt(giamgia));
                Vector vDSKMTN = new Vector();
                vDSKMTN = kmm.LayDSKMTN(Integer.parseInt(makm));
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "promotion");
                request.setAttribute("DSKMTN", vDSKMTN);
                request.setAttribute("makm", makm);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/detailpromotionday.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("updatepromotionbill")){
                String makm = request.getParameter("makm");
                String datestart = request.getParameter("datestart");
                String enddate = request.getParameter("enddate");
                String promotionname = request.getParameter("promotionname");
                String content = request.getParameter("content");
                String condition = request.getParameter("condition");
                String discount = request.getParameter("discount");
                String item = request.getParameter("item");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                KhuyenMai_Model.updatepromotionbill( Integer.parseInt(makm), datestart, enddate, promotionname, content);
                KhuyenMai_Model.updatedetailpromotionbill( Integer.parseInt(makm), condition, discount);
                Vector v = new Vector();
                v = kmm.LayDSKM(langid);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "promotionbill");
                request.setAttribute("DSKM", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listpromotion.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("UpdateDetailPromotionDay")){
                String makm = request.getParameter("makm");
                String datestart = request.getParameter("datestart");
                String enddate = request.getParameter("enddate");
                String promotionname = request.getParameter("promotionname");
                String content = request.getParameter("content");
                String item = request.getParameter("item");
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                KhuyenMai_Model.updatepromotionbill( Integer.parseInt(makm), datestart, enddate, promotionname, content);
                Vector v = new Vector();
                v = kmm.LayKMEdit(Integer.parseInt(makm), langid);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "promotionday");
                request.setAttribute("item", item);
                request.setAttribute("makm", makm);
                request.setAttribute("KM", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/prodate.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("CheckDate")) {
                String tungay = request.getParameter("tungay");
                String denngay = request.getParameter("denngay");
                if(KhuyenMai_Model.CheckDate(tungay, denngay)){
                    out.print("true");
                }
                else{
                    out.print("false");
                }
            }
            else if (action.equals("CheckDateKM")) {
                String tungay = request.getParameter("tungay");
                String denngay = request.getParameter("denngay");
                String loaikm = request.getParameter("loaikm");                
                if(KhuyenMai_Model.CheckDateKM(tungay, denngay, loaikm)){
                    out.print("true");
                }
                else{
                    out.print("false");
                }
            }
            else if (action.equals("CheckDateKMUpdate")) {
                String tungay = request.getParameter("tungay");
                String denngay = request.getParameter("denngay");
                String loaikm = request.getParameter("loaikm");
                String makm = request.getParameter("makm");
                if(KhuyenMai_Model.CheckDateKMUpdate(tungay, denngay, loaikm, makm)){
                    out.print("true");
                }
                else{
                    out.print("false");
                }
            }
            else if (action.equals("DeleteKM")) {
                String makm = request.getParameter("makm");
                String item = request.getParameter("item");
                String MaKhuyenMai[] = makm.split("-");
                int j = 0;
                while (j < MaKhuyenMai.length) {
                    KhuyenMai_Model.DeleteKM(MaKhuyenMai[j]);                    
                    j = j + 1;
                }
                KhuyenMai_Model kmm = new KhuyenMai_Model();
                Vector v = new Vector();
                v = kmm.LayDSKM(langid);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(4, nv.getMaNV(), "promotion");
                request.setAttribute("DSKM", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listpromotion.jsp");
                rd.forward(request, response);
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
